//
//  MKMainTabBar.swift
//  MKWeiBo2017
//
//  Created by nicolas on 2017/5/9.
//  Copyright © 2017年 nicolas. All rights reserved.
//

import UIKit

class MKMainTabBar: UITabBar {

    var composeCallBack: (() -> ())?
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let width = self.frame.width / 5
        
        var index = 0
        
        for view in self.subviews {
            if view.isKind(of: NSClassFromString("UITabBarButton")!) {
                view.frame = CGRect(x: CGFloat(index)*width, y: 0, width: width, height: self.frame.height)
            
               index += 1
                
                if index == 2 {
                    index += 1
                }
            }
        }
        self.addSubview(composeButton)
        composeButton.frame = CGRect(x: 2 * width, y: 0, width: width, height: self.frame.height)
    }
    
    func composeButtonClick(button:UIButton) {
        
        composeCallBack!()
    }
    
    lazy var composeButton: UIButton = {
        
        let button = UIButton(type: UIButtonType.custom)
        
        button.setBackgroundImage(UIImage(named:"tabbar_compose_button"), for: UIControlState.normal)
      button.setBackgroundImage(UIImage(named:"tabbar_compose_button_highlighted"), for: UIControlState.highlighted)
        button.setImage(UIImage(named:"tabbar_compose_icon_add"), for: UIControlState.normal)
        button.setImage(UIImage(named:"tabbar_compose_icon_add_highlighted"), for: UIControlState.highlighted)
        
        button.addTarget(self, action: #selector(composeButtonClick), for: .touchUpInside)
        
        return button
        
    }()
    

}
